﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using System.ComponentModel.Composition;
using System.IO;
using System.Data;
using System.Diagnostics.CodeAnalysis;

namespace Steve.EFDataLayer
{
    [Export(typeof(MetaRepository))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class EFMetaRepository : MetaRepository
    {
        static EFMetaRepository()
        {
            SteveEFDataLayer.SetupAutoMapper();
        }

        public override IEnumerable<ExternalSystem> GetRecentImports(int count)
        {
            using (var ctx = new SteveEntities())
            {
                var systemQuery = (from s in ctx.DbSystems
                                   where s.LastImportDate != null
                                   orderby s.LastImportDate descending
                                   select s).Take(count);

                return systemQuery.ToList().Select(s => Mapper.Map<DbSystem, ExternalSystem>(s));
            }
        }

        public override IEnumerable<DataUpdate> GetRecentUpdates(int count)
        {
            using (var ctx = new SteveEntities())
            {
                var dataUpdateQuery = (from u in ctx.DbDataUpdates
                                       orderby u.RevisionDate descending
                                       select u).Take(count);

                return dataUpdateQuery.ToList().Select(s => Mapper.Map<DbDataUpdate, DataUpdate>(s));
            }
        }
    }
}
